import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/erp_odd_numbers_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_details_entity.dart';
import 'package:xiongan_app_yy/entrys/rp/return_booking_entity.dart';
import 'package:xiongan_app_yy/provider/erp_info.dart';
import 'package:xiongan_app_yy/widgets/inv_list_widget.dart';

class StorageDetailsPage extends StatefulWidget {
  final Map<String, dynamic> args;

  @override
  State<StatefulWidget> createState() {
    return StorageDetailsPageState();
  }

  StorageDetailsPage(this.args);
}

class StorageDetailsPageState extends State<StorageDetailsPage> {
  //ErpOddNumbersData args;
  int size = 0;
  ErpOddNumbersData mode;
  InvListDetailsDatas data;
  ReturnBookingEntity rbean;
  bool show = false;

  @override
  Widget build(BuildContext context) {
    //args = ModalRoute.of(context).settings.arguments;
    if (widget.args["type"] == "push") {
      if (widget.args["title"] == "结余退库物资明细") {
        rbean = Provider.of<ErpInfo>(context).rbean;
        size = Provider.of<ErpInfo>(context).rbean.outOrderDetailList.length;
      } else {
        mode = widget.args["data"];
        size = Provider.of<ErpInfo>(context).mode.details.length;
      }
    } else if (widget.args["type"] == "pull") {
      data = Provider.of<ErpInfo>(context).data;
      size = Provider.of<ErpInfo>(context).data.taskDetailList.length;
    }
    if (widget.args["show"] != null) {
      show = widget.args["show"];
    }
    return Scaffold(
        appBar: new AppBar(
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            widget.args["title"],
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: Column(
          children: <Widget>[
            Expanded(
              child: ListView.separated(
                  itemBuilder: (BuildContext context, int index) {
                    //ErpOddNumbersDatasDetail data=args.details.elementAt(index);
                    return InvListWidget(
                        context: context,
                        index: index,
                        data: mode != null ? mode.details[index] : null,
                        dataDetail: data != null ? data.taskDetailList[index] : null,
                        rDetail: rbean != null ? rbean.outOrderDetailList[index] : null,
                        title: widget.args["title"],
                        type: widget.args["type"]);
                  },
                  separatorBuilder: (BuildContext context, int index) =>
                      Divider(height: 1, color: Color(0xffdddddd)),
                  itemCount: size),
            ),
            Visibility(
              visible: show,
              child: GestureDetector(
                child: Container(
                  width: double.infinity,
                  height: 48,
                  color: Colors.blue,
                  child: Center(
                      child: Text(
                    "确定",
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  )),
                ),
                onTap: () {
                  if (widget.args["type"] == "push") {
                    if (widget.args["title"] == "结余退库物资明细") {
                      for (int i = 0;
                          i < rbean.outOrderDetailList.length;
                          i++) {
                        Provider.of<ErpInfo>(context)
                                .rbean
                                .outOrderDetailList[i]
                                .realquantity =
                            rbean.outOrderDetailList[i].realquantity;
                      }
                    } else {
                      for (int i = 0; i < mode.details.length; i++) {
                        Provider.of<ErpInfo>(context).mode.details[i].qty = mode.details[i].qty;
                      }
                    }
                  } else if (widget.args["type"] == "pull") {
                    for (int i = 0; i < data.taskDetailList.length; i++) {
                      Provider.of<ErpInfo>(context).data.taskDetailList[i].qty = data.taskDetailList[i].qty;
                    }
                  }
                  Navigator.pop(context, "ok");
                },
              ),
            )
          ],
        ));
  }
}
